<template>
  <a-modal title-align="start" title="添加属性值" :mask-closable="false" v-model:visible="showvisible" @cancel="showvisible = false" :width="600">
    <a-form auto-label-width ref="formRef" size="medium" :model="form">
      <a-form-item field="name" label="名称" :rules="rules.name">
        <a-input v-model="form.name" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item field="slug" label="标识" :rules="rules.slug">
        <a-input v-model="form.slug" placeholder="请输入标识" />
      </a-form-item>
    </a-form>

    <template #footer class="modal-footer">
      <a-button @click="showvisible = false">取消</a-button>
      <a-button type="primary" :loading="loading" @click="submit">确定</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref, computed } from 'vue'
  import { batchAttributeTerm } from '@/api/product'
  import { Message } from '@arco-design/web-vue'
  let props = defineProps(['modelValue', 'shopId', 'id'])
  let emits = defineEmits(['save', 'update:modelValue'])
  const formRef = ref()
  let showvisible = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      emits('update:modelValue', val)
    },
  })
  let form = ref({
    name: '',
    slug: '',
  })
  let loading = ref(false)
  let rules = ref({
    name: [{ required: true, message: '请输入名称' }],
    slug: [{ required: true, message: '请输入标识' }],
  })

  const submit = async () => {
    const valid = await formRef.value.validate()
    if (valid) return
    try {
      let params = {
        param: {
          create: [{ ...form.value }],
        },
        shopId: props.shopId,
        productAttributeId: props.id,
      }
      loading.value = true
      const res: any = await batchAttributeTerm(params)
      if (res) {
        Message.success('添加成功')
        emits('save', form.value.name)
        showvisible.value = false
      }
    } catch (error) {}
    loading.value = false
  }
</script>

<style lang="less" scoped></style>
